<!--
Date: 2025-08-13
Copyright (c) 2025 Industrial Software Feature Database
-->
<template>
  <el-dialog v-model="dialogVisible" title="部段编码设置" width="800" :before-close="handleClose" append-to-body>
    <div class="text">生成强度数模清单前，需要进行部段编码设置。</div>
    <div class="text">以下为本次选中数据的全部部段，请对其进行部段编码设置</div>
    <div class="text">
      <el-row :gutter="10">
        <el-col :span="12">所属机型：{{ data.aircraftModel }}</el-col>
        <el-col :span="12">所属批次：{{ data.track }}</el-col>
      </el-row>
    </div>
    <el-table v-loading="loading" :data="tableDatas">
      <el-table-column prop="name" label="部段名称" />
      <el-table-column prop="code" label="部段编码">
        <template #default="scope">
          <el-input v-model="scope.row.code" placeholder="请输入" :disabled="scope.row.disabled"></el-input>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <div class="dialog-footer text-align-center">
        <el-button v-loading="buttonLoading" @click="handleClose">返 回</el-button>
        <el-button v-loading="buttonLoading" type="primary" @click="handleSave"> 保存并生成强度数模清单</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="sectionCodeSettingDialog" lang="ts">
import { listTree, generateStrengthV2 } from '@/api/bomCompareTool/standardInstallationList/index';
import { getEventInfo } from '@/api/bomCompareTool/originInstallationList/index';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const emit = defineEmits(['on-sure']);
const dialogVisible = ref(false);
const buttonLoading = ref(false);
const data = reactive({
  aircraftModel: '',
  track: ''
});
const tableDatas = ref([]);
const loading = ref(false);
const handleClose = () => {
  dialogVisible.value = false;
};
const openDialog = async (datas) => {
  data.track = datas.track;
  data.aircraftModel = datas.aircraftModel;
  const res = await listTree();
  tableDatas.value = datas.tableDatas.map((item) => {
    return {
      id: item.id,
      name: item.section,
      code: '',
      disabled: false
    };
  });
  const obj = res.data.find((item) => item.name === data.aircraftModel);
  if (obj && obj.children) {
    const newObj = obj.children.find((item) => item.name === data.track);
    if (newObj && newObj.children) {
      tableDatas.value.forEach((item) => {
        const newObjs = newObj.children.find((el) => el.name === item.name);
        if (newObjs) {
          item.code = newObjs.mark;
          item.disabled = true;
        }
      });
    }
  }
  dialogVisible.value = true;
};
const handleSave = async () => {
  const datas = tableDatas.value.find((item) => !item.code);
  if (datas) {
    proxy?.$modal.msgWarning('请填写完整信息!');
    return false;
  }
  buttonLoading.value = true;
  const obj = {
    aircraftModel: data.aircraftModel,
    standardBomIds: tableDatas.value.map((item) => item.id),
    sectionCodeNames: tableDatas.value.map((item) => {
      return {
        name: item.name,
        code: item.code
      };
    })
  };
  const res = await generateStrengthV2(obj);
  const timer = setInterval(async () => {
    const ids = res.data.join(',');
    const datas = await getEventInfo({ ids: ids }).catch(() => {
      clearInterval(timer);
      buttonLoading.value = false;
    });
    datas.forEach((item, index) => {
      proxy?.$modal.msg(`${item.currentInfo}`);
      if (index === datas.length - 1 && (item.status === 'DONE' || item.status === 'FAIL' || item.status === 'NONE_MATCH')) {
        clearInterval(timer);
        emit('on-sure');
        proxy?.$modal.msgSuccess('生成强度数模清单成功!');
        dialogVisible.value = false;
        buttonLoading.value = false;
      }
    });
  }, 1000);
};
defineExpose({
  openDialog
});
</script>

<style lang="scss" scoped>
.text {
  line-height: 28px;
}
.dialog-footer {
  text-align: center;
}
</style>
